<!DOCTYPE html>
<html>
<head>
    <title>养基宝 API 调试测试</title>
    <meta charset="utf-8">
</head>
<body>
    <h1>养基宝 API 调试测试</h1>
    <button id="testConnection">测试连接</button>
    <button id="testWithToken">使用Token测试</button>
    <div id="results"></div>

    <script>
        // 复制background.js中的MD5函数
        function md5(str) {
            function utf8Encode(str) {
                let result = "";
                for (let i = 0; i < str.length; i++) {
                    const charcode = str.charCodeAt(i);
                    if (charcode < 0x80) result += String.fromCharCode(charcode);
                    else if (charcode < 0x800) {
                        result += String.fromCharCode(0xc0 | (charcode >> 6));
                        result += String.fromCharCode(0x80 | (charcode & 0x3f));
                    } else {
                        result += String.fromCharCode(0xe0 | (charcode >> 12));
                        result += String.fromCharCode(0x80 | ((charcode >> 6) & 0x3f));
                        result += String.fromCharCode(0x80 | (charcode & 0x3f));
                    }
                }
                return result;
            }
            
            str = utf8Encode(str);
            let hash = 0;
            for (let i = 0; i < str.length; i++) {
                hash = ((hash << 5) - hash + str.charCodeAt(i)) & 0xFFFFFFFF;
            }
            return (hash >>> 0).toString(16).padStart(8, '0');
        }

        const API_SECRET = "YxmKSrQR4uoJ5lOoWIhcbd7SlUEh9OOc";
        const API_BASE_URL = "http://browser-plug-api.yangjibao.com";

        function log(message) {
            const results = document.getElementById('results');
            results.innerHTML += '<div>' + new Date().toLocaleTimeString() + ': ' + message + '</div>';
        }

        async function testConnection() {
            log('开始测试基本连接...');
            try {
                const response = await fetch(API_BASE_URL + '/index_data', {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });
                log('响应状态: ' + response.status);
                const data = await response.text();
                log('响应内容: ' + data.substring(0, 200) + '...');
            } catch (error) {
                log('连接错误: ' + error.message);
            }
        }

        async function testWithToken() {
            log('开始测试带签名的请求...');
            const token = '';
            const requestTime = Math.floor(Date.now() / 1000);
            const url = '/index_data';
            
            const pathname = new URL(API_BASE_URL).pathname || '';
            const urlPath = url.split('?')[0];
            const signString = pathname + urlPath + token + requestTime + API_SECRET;
            const requestSign = md5(signString);
            
            log('签名字符串: ' + signString);
            log('生成的签名: ' + requestSign);
            
            try {
                const response = await fetch(API_BASE_URL + url, {
                    method: 'GET',
                    headers: {
                        'Content-Type': 'application/json',
                        'Request-Time': requestTime.toString(),
                        'Authorization': token,
                        'Request-Sign': requestSign
                    }
                });
                log('响应状态: ' + response.status);
                const data = await response.text();
                log('响应内容: ' + data.substring(0, 200) + '...');
            } catch (error) {
                log('请求错误: ' + error.message);
            }
        }

        document.getElementById('testConnection').addEventListener('click', testConnection);
        document.getElementById('testWithToken').addEventListener('click', testWithToken);
    </script>
</body>
</html>
